import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:myflutterproject/basematerialwidgetsdemo.dart';

class CupertinoSwitchDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => CupertinoSwitchState();
}

class CupertinoSwitchState extends State<CupertinoSwitchDemo> {
  bool _switchValue = false;
  bool _switchValue2 = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('CupertinoSwitch'),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            TextWidget('Normal Switch'),
            _normalSwitch(),
            TextWidget('Custom Switch'),
            _customSwitch()
          ],
        ),
      ),
    );
  }

  _normalSwitch() {
    return CupertinoSwitch(
        //当前的开关
        value: _switchValue,
        //点击或者拖拽事件
        onChanged: (value) {
          setState(() {
            _switchValue = value;
          });
        });
  }
  _customSwitch() {
    return CupertinoSwitch(
        //当前的开关
        value: _switchValue2,
        //打开时的颜色
        activeColor: Colors.orange,
        //关闭时候的颜色
        trackColor: Colors.grey,
        //拖拽效果 默认为 start 更为平滑，为 down 时有明显吸附效果
        dragStartBehavior: DragStartBehavior.down,
        //点击或者拖拽事件
        onChanged: (value) {
          setState(() {
            _switchValue2 = value;
          });
        });
  }
}
